<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input v-model="listQuery.keyword" class="input-width" placeholder="帐号/姓名" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="帐号" align="center">
          <template slot-scope="scope">{{scope.row.username}}</template>
        </el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope"><img style="width: 5vw;" :src="scope.row.icon" alt="" srcset=""></template>
        </el-table-column>
        <el-table-column label="昵称" align="center">
          <template slot-scope="scope">{{scope.row.nickname}}</template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">{{scope.row.gender==0? "未知":scope.row.gender==1?"男":"女"}}</template>
        </el-table-column>
        <el-table-column label="手机号码" align="center">
          <template slot-scope="scope">{{scope.row.phone}}</template>
        </el-table-column>
        <el-table-column label="注册时间" width="160" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="是否启用"  align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="handleSelect1(scope.$index, scope.row)">
              信息统计
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleSelect(scope.$index, scope.row)">
              查看资料
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑用户':'添加用户'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="admin"
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item label="帐号：">
          <el-input v-model="admin.username" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="昵称：">
          <el-input v-model="admin.nickname" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="手机号码：">
          <el-input v-model="admin.phone" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="密码：">
          <el-input v-model="admin.password"  type="password" style="width: 250px"></el-input>
        </el-form-item>
       <!-- <el-form-item v-if="isEdit" label="积分：">
          <el-input v-model="admin.integration"  type="text" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item v-if="isEdit" label="抽奖次数：">
          <el-input v-model="admin.luckeyCount"  type="text" style="width: 250px"></el-input>
        </el-form-item> -->
        <el-form-item label="是否启用：">
          <el-radio-group v-model="admin.status">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="头像">
            <single-upload :isEdit="admin.icon==null || admin.icon==''" v-model="admin.icon"
                           style="width: 300px;display: inline-block;margin-left: 10px"></single-upload>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="会员资料"
      :visible.sync="allocDialogVisible"
      width="50%">
      <el-form :model="member"
      label-position="left"
               label-width="150px" size="small">
               <el-form-item label="账号：">
          {{member.username}}
        </el-form-item>
        <el-form-item label="昵称：">
          {{member.nickname}}
        </el-form-item>
        <el-form-item label="手机号码：">
          {{member.phone}}
        </el-form-item>
         <el-form-item label="注册时间：">
          {{member.createTime | formatDateTime}}
        </el-form-item>
        <el-form-item label="所在城市：">
          <span style="display: inline-block;width: 15vw;">{{member.city}}</span>
         <span class="biaoti">性别：</span> {{member.gender==0? "未知":member.gender==1?"男":"女"}}
        </el-form-item>
        <el-form-item label="职业：">
          <span style="display: inline-block;width: 15vw;">{{member.job}}</span>
         <span class="biaoti">生日：</span> {{member.birthday | formatDateTimeBirthday}}
        </el-form-item>
       <!-- <el-form-item label="剩余抽奖次数：">
          <span style="display: inline-block;width: 15vw;">{{member.luckeyCount}}</span>
          <span class="biaoti">成长值：</span>{{member.growth}}
        </el-form-item>
        <el-form-item label="历史积分数量：">
          <span style="display: inline-block;width: 15vw;">{{member.historyIntegration}}</span>
          <span class="biaoti">积分：</span>{{member.integration}}
        </el-form-item> -->
        <el-form-item label="个性签名：">
          {{member.personalizedSignature}}
        </el-form-item>
      </el-form>
      <img class="img" :src="member.icon" alt="" srcset="">
      <span slot="footer" class="dialog-footer">
        <el-button @click="allocDialogVisible = false" size="small">关 闭</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="会员信息统计"
      :visible.sync="allocDialogVisible1"
      width="50%">
      <el-form :model="statisticsInfo"
      label-position="left"
               label-width="150px" size="small">
               <el-form-item label="累计消费金额：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.consumeAmount}}</span>
          <span class="biaoti">累计订单数量：</span>{{statisticsInfo.orderCount}}
        </el-form-item>
       <!-- <el-form-item label="优惠券数量：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.couponCount}}</span>
          <span class="biaoti">评价数：</span>{{statisticsInfo.commentCount}}
        </el-form-item> -->

        <el-form-item label="退货数量：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.returnOrderCount}}</span>
         <span class="biaoti">登录次数：</span> {{statisticsInfo.loginCount}}
        </el-form-item>
       <!-- <el-form-item label="关注数量：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.attendCount}}</span>
         <span class="biaoti">粉丝数量：</span> {{statisticsInfo.fansCount}}
        </el-form-item> -->
        <el-form-item label="产品收藏：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.collectProductCount}}</span>
        <!--  <span class="biaoti">专题收藏：</span>{{statisticsInfo.collectSubjectCount}} -->
        </el-form-item>
        <!-- <el-form-item label="话题收藏：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.collectTopicCount}}</span>
          <span class="biaoti">评论收藏：</span>{{statisticsInfo.collectCommentCount}}
        </el-form-item> -->
        <!-- <el-form-item label="最后一次下订单：">
          <span style="display: inline-block;width: 15vw;">{{statisticsInfo.recentOrderTime | formatDateTimeBirthday}}</span>
          <span class="biaoti">邀请朋友：</span>{{statisticsInfo.inviteFriendCount}}
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="allocDialogVisible1 = false" size="small">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {fetchList,createAdmin,updateAdmin,updateStatus,deleteAdmin,getRoleByAdmin,getById,statisticsGetById} from '@/api/member';
  import {fetchAllRoleList} from '@/api/role';
  import {formatDate} from '@/utils/date';
  import SingleUpload from '@/components/Upload/singleUpload'
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    keyword: null
  };
  const defaultAdmin = {
    id: null,
    username: null,
    password: null,
    nickName: null,
    email: null,
    note: null,
    status: 1
  };
  export default {
    name: 'adminList',
    components: {SingleUpload},
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        admin: Object.assign({}, defaultAdmin),
        isEdit: false,
        allocDialogVisible: false,
        allocDialogVisible1:false,
        member:[],
        statisticsInfo:[],
        allRoleList:[],
        allocAdminId:null
      }
    },
    created() {
      this.getList();

    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatDateTimeBirthday(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.admin = Object.assign({},defaultAdmin);
      },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus(row.id, {status: row.status}).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAdmin(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.admin = Object.assign({},row);
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            updateAdmin(this.admin.id,this.admin).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            createAdmin(this.admin).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },
      handleAllocDialogConfirm(){
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = new URLSearchParams();
          params.append("adminId", this.allocAdminId);
          params.append("roleIds", this.allocRoleIds);
          allocRole(params).then(response => {
            this.$message({
              message: '分配成功！',
              type: 'success'
            });
            this.allocDialogVisible = false;
          })
        })
      },
      handleSelect(index,row){
        this.member= row;
        this.allocDialogVisible = true;
      },
      handleSelect1(index,row){
        statisticsGetById(row.id).then(res=>{
        this.statisticsInfo= res.data;
        this.allocDialogVisible1 = true;
        })

      },
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  }
</script>
<style scoped>
.biaoti{
    width: 8vw;
    display: inline-block;
    margin-right: 1vw;
}
.img{
      width: 13vw;
    position: absolute;
    top: 5vw;
    right: 6vw;
}
</style>
